package main

import "fmt"

func main() {
	var n int
	fmt.Println("请输入数组的行列数:")
	fmt.Scan(&n)
	arr := make([][]int, n)
	for i := range arr {
		arr[i] = make([]int, n)
	}
	num := 1
	left, right, top, bottom := 0, n-1, 0, n-1

	for num <= n*n {
		for i := left; i <= right; i++ {
			arr[top][i] = num
			num++
		}
		top++
		for i := top; i <= bottom; i++ {
			arr[i][right] = num
			num++
		}
		right--
		for i := right; i >= left; i-- {
			arr[bottom][i] = num
			num++
		}
		bottom--
		for i := bottom; i >= top; i-- {
			arr[i][left] = num
			num++
		}
		left++
	}

	for _, row := range arr {
		for _, num := range row {
			fmt.Printf("%3d ", num)
		}
		fmt.Println()
	}
}
